package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;

import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
public class AddUnitServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private String name="";
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/xml");
        PrintWriter out = response.getWriter();
        name=request.getParameter("name");
        addUnit(name);
        String resultXML=createXML(name);
        out.println(resultXML);
    }
    protected void addUnit(String _name)
    {
    	String sql="";
    	try
    	{
    	Connection con; 
        Statement st;
     //   ResultSet rs;
        Class.forName("org.postgresql.Driver");
        //cho nay anh doi ten database,username va pass lai cho dung voi may cua anh
        con=DriverManager.getConnection("jdbc:postgresql://localhost/buzzcomn","postgres","root");
        st = con.createStatement();
        sql="select sp_unit_insert('"+_name+"',null,null,null,null,null,null,null,null)";
        st.executeQuery(sql);
        st.close();
        con.close();
    	}
    	catch (Exception e){}
        
    }
    protected String createXML(String _name)
    {
    	try
    	{
    		
    		 DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
             DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
             Document doc = docBuilder.newDocument();
             Element unit = doc.createElement("unit");
             doc.appendChild(unit);
             Element e_name = doc.createElement("name");
             unit.appendChild(e_name);
             Text t_name = doc.createTextNode(_name);
             e_name.appendChild(t_name);
             
             TransformerFactory transfac = TransformerFactory.newInstance();
             Transformer trans = transfac.newTransformer();
             trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
             trans.setOutputProperty(OutputKeys.INDENT, "yes");

             //create string from xml tree
             StringWriter sw = new StringWriter();
             StreamResult result = new StreamResult(sw);
             DOMSource source = new DOMSource(doc);
             trans.transform(source, result);
             String xmlString = sw.toString();

             
             return xmlString;
             
             
             
            

         } 
    	 catch (Exception e) 
         {
             return "error";
         }
	}

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }


    @Override
    public String getServletInfo() {
        return "Short description";
    }

}

